package com.android.love.ui;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.android.love.R;
import com.android.love.adapter.MeixiuAdapter;
import com.android.love.adapter.MeixiuAdapter_1;
import com.android.love.net.UriHelper;
import com.android.love.util.ActivityUtil;
import com.android.love.util.LogUtil;
import com.android.love.view.GridViewWithHeaderBaseAdapter.GridItemClickListener;
import com.android.love.vo.Meixiu;
import com.android.love.vo.MyRequest;
import com.github.kevinsawicki.wishlist.ViewFinder;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.huewu.pla.lib.internal.PLA_AdapterView;

public class MeixiuActivity extends BasePageActivity implements
		GridItemClickListener {

	private ViewFinder meixiuFider;
	/** 美秀列表 **/
	private List<Meixiu> meixius;
	private PullToRefreshListView mPullToRefreshGridView;
	private ListView mGridView;
	private MeixiuAdapter_1 mMeixiuAdapter;
	/** 页数 **/
	private int page = 1;
	/** 是否弹出加载中提示 **/
	private boolean show = true;
	private View footView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		View meixiuView = inflater.inflate(R.layout.activity_meixiu, null);
		setContentView(meixiuView);
		meixiuFider = new ViewFinder(meixiuView);
		process();
		setListener();
		getData();
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		LogUtil.i(TAG, "onNewIntent");
	}

	@Override
	protected void onResume() {
		mApplication.setDefaultIndex(MEIXIU);
		super.onResume();
		LogUtil.i(TAG, "onResume");
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	public void handle(int tag, Object object) {
		super.handle(tag, object);
		switch (tag) {
		case MEIXIU:
			if (footView != null)
				footView.setVisibility(View.GONE);
			if (object != null) {
				LogUtil.i(TAG, "success");
				List<Meixiu> meixius_list = (List<Meixiu>) object;
				if (meixius_list != null && meixius_list.size() > 0) {
					updatemeixius(meixius_list);
				} else {
					ActivityUtil.show(MeixiuActivity.this, "没有更多数据");
				}
			} else {
				page--;
			}
			mPullToRefreshGridView.onRefreshComplete();
			break;

		default:
			break;
		}

	}

	private void updatemeixius(List<Meixiu> result) {
		// TODO Auto-generated method stub
		meixius.addAll(result);
		if (mMeixiuAdapter == null) {
			mMeixiuAdapter = new MeixiuAdapter_1(context, loader, meixius);
			mMeixiuAdapter.setNumColumns(2);
			mMeixiuAdapter.setOnGridClickListener(this);
			mGridView.setAdapter(mMeixiuAdapter);
		} else {
			mMeixiuAdapter.notifyDataSetChanged();
		}

	}

	@Override
	protected void process() {
		super.process();
		meixius = new ArrayList<Meixiu>();
		mPullToRefreshGridView = (PullToRefreshListView) findViewById(R.id.pull_refresh_meixiu);
		mGridView = mPullToRefreshGridView.getRefreshableView();
		meixiuFider.setDrawable(R.id.iv_top_meinv, R.drawable.meinv);
		meixiuFider.setText(R.id.top_title, "美秀");
		hide(meixiuFider.find(R.id.iv_shuaxin));
		footView = LayoutInflater.from(context).inflate(R.layout.loading_item,
				null);
		TextView textView = (TextView) footView.findViewById(R.id.tv_loading);
		textView.setText("正在加载.....");
		mGridView.addFooterView(footView);
		footView.setVisibility(View.GONE);
	}

	@Override
	protected void getData() {
		super.getData();
		MyRequest req = new MyRequest();
		req.authority = UriHelper.REALM_NAME;
		req.path = UriHelper.URL_ALBUM;
		req.paramers = generateParams(page);
		getDataFromTask(context, MEIXIU, req, meixius, show, true);
	}

	@Override
	protected void setListener() {
		super.setListener();
		mPullToRefreshGridView.setMode(Mode.DISABLED);
		mPullToRefreshGridView.setScrollbarFadingEnabled(true);
		// mPullToRefreshGridView
		// .setOnRefreshListener(new OnRefreshListener<GridView>() {
		//
		// @Override
		// public void onRefresh(
		// PullToRefreshBase<GridView> refreshView) {
		// show = false;
		// page++;
		// getData();
		//
		// }
		// });
		mPullToRefreshGridView
				.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {

					@Override
					public void onLastItemVisible() {
						LogUtil.i(TAG, "OnLastItemVisibleListener....");
						if (footView != null)
							footView.setVisibility(View.VISIBLE);
						show = false;
						page++;
						getData();
					}
				});
		// mGridView
		// .setOnItemClickListener(new PLA_AdapterView.OnItemClickListener() {
		//
		// @Override
		// public void onItemClick(PLA_AdapterView<?> parent,
		// View view, int position, long id) {
		// Intent intent = new Intent(context,
		// ViewpagerActivity.class);
		// // if (position - 1 >= 0)
		// // // intent.putExtra("album_id",
		// // // meixius.get(position - 1).getAlbum_id());
		// // intent.putExtra("meixiu", meixius.get(position - 1));
		// // else
		// // intent.putExtra("album_id", meixius.get(position)
		// // .getAlbum_id());
		// intent.putExtra("meixiu", meixius.get(position));
		// startActivity(intent);
		// }
		// });
	}

	/**
	 * 得到传递的参数
	 * 
	 * @return
	 * @throws JSONException
	 * @throws IOException
	 */
	public Map<String, String> generateParams(int page) {
		JSONObject json = new JSONObject();
		TreeMap<String, String> params = new TreeMap<String, String>();
		params.put("app_key", "android");
		params.put("v", "1.0");
		params.put("page", page + "");
		params.put("count", "10");
		if (sp.getValue(USER_ID, 0) != 0)
			params.put("user_id", sp.getValue("user_id", 0) + "");
		if (!"".equals(sp.getValue(SIG, "")))
			params.put("sig", sp.getValue("sig", ""));
		return params;
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
	}

	@Override
	public void onGridItemClicked(View v, int position, long itemId) {
		Intent intent = new Intent(context, ViewpagerActivity.class);
		// if (position - 1 >= 0)
		// // intent.putExtra("album_id",
		// // meixius.get(position - 1).getAlbum_id());
		// intent.putExtra("meixiu", meixius.get(position - 1));
		// else
		// intent.putExtra("album_id", meixius.get(position)
		// .getAlbum_id());
		intent.putExtra("meixiu", meixius.get(position));
//		startActivity(intent);
		intent.putExtra("position", position);
		startActivityForResult(intent, 1);
	}
	
	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		super.onActivityResult(arg0, arg1, arg2);
		if(arg0==1){
			if(arg2!=null){
				LogUtil.e(TAG, "onActivityResult");
				int position=arg2.getIntExtra("position", -1);
				if(position!=-1){
					Meixiu meixiu=(Meixiu) arg2.getSerializableExtra("meixiu");
					meixius.remove(position);
					meixius.add(position, meixiu);
					mMeixiuAdapter.notifyDataSetChanged();
				}
			}
		}
	}

}
